<template>
  <div class="version-manager-container">
    <el-row :gutter="24">
      <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="12" class="mb-20">
        <el-card shadow="hover" class="version-card">
          <div class="card-header">
            <div class="header-left">
              <i class="el-icon-mobile-phone header-icon"></i>
              <div class="header-title">
                <h3>司机模块</h3>
                <div class="version-tag">当前版本：<el-tag size="small" type="success">{{ driverVersion }}</el-tag></div>
              </div>
            </div>
          </div>
          <el-divider></el-divider>
          <el-form ref="driverForm" :model="driverForm" :rules="rules" label-width="100px" class="version-form">
            <el-form-item label="版本号" prop="version">
              <el-input v-model="driverForm.version" placeholder="请输入版本号" prefix-icon="el-icon-document-checked" />
            </el-form-item>
            <el-form-item label="上传文件">
              <el-upload
                class="upload-container"
                action="https://www.jrzrescue.com/api/version/upload"
                list-type="text"
                :on-success="handleDriverFileSuccess"
                :file-list="driverForm.fileList"
              >
                <el-button type="primary" icon="el-icon-upload">选择文件</el-button>
                <div class="upload-tip" v-if="driverForm.fileList.length === 0">请上传应用安装包</div>
              </el-upload>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" class="submit-btn" icon="el-icon-check" @click="submitDriverForm">发布新版本</el-button>
            </el-form-item>
          </el-form>
        </el-card>
      </el-col>
      
      <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="12" class="mb-20">
        <el-card shadow="hover" class="version-card">
          <div class="card-header">
            <div class="header-left">
              <i class="el-icon-s-platform header-icon"></i>
              <div class="header-title">
                <h3>调度模块</h3>
                <div class="version-tag">当前版本：<el-tag size="small" type="success">{{ diaoduVersion }}</el-tag></div>
              </div>
            </div>
          </div>
          <el-divider></el-divider>
          <el-form ref="diaoduForm" :model="diaoduForm" :rules="rules" label-width="100px" class="version-form">
            <el-form-item label="版本号" prop="version">
              <el-input v-model="diaoduForm.version" placeholder="请输入版本号" prefix-icon="el-icon-document-checked" />
            </el-form-item>
            <el-form-item label="上传文件">
              <el-upload
                class="upload-container"
                action="https://www.jrzrescue.com/api/version/upload"
                list-type="text"
                :on-success="handleDiaoduFileSuccess"
                :file-list="diaoduForm.fileList"
              >
                <el-button type="primary" icon="el-icon-upload">选择文件</el-button>
                <div class="upload-tip" v-if="diaoduForm.fileList.length === 0">请上传应用安装包</div>
              </el-upload>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" class="submit-btn" icon="el-icon-check" @click="submitDiaoduForm">发布新版本</el-button>
            </el-form-item>
          </el-form>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import {
  addVersion,
  getDriverVersion,
  getDiaoduVersion,
  addDiaoduVersion
} from "../../../api/api/rescue";

export default {
  name: "VersionManager",
  data() {
    return {
      driverVersion: '',
      diaoduVersion: '',
      driverForm: {
        version: '',
        fileList: []
      },
      diaoduForm: {
        version: '',
        fileList: []
      },
      rules: {
        version: [{ required: true, message: "版本号不能为空", trigger: "blur" }],
        file: [{ required: true, message: "文件不能为空", trigger: "change" }],
      },
    };
  },
  created() {
    this.fetchDriverVersion();
    this.fetchDiaoduVersion();
  },
  methods: {
    fetchDriverVersion() {
      getDriverVersion().then(response => {
        this.driverVersion = response.data.version;
      });
    },
    fetchDiaoduVersion() {
      getDiaoduVersion().then(response => {
        this.diaoduVersion = response.data.version;
      });
    },
    handleDriverFileSuccess(response, file, fileList) {
      this.driverForm.fileList = fileList;
    },
    handleDiaoduFileSuccess(response, file, fileList) {
      this.diaoduForm.fileList = fileList;
    },
    submitDriverForm() {
      this.$refs.driverForm.validate((valid) => {
        if (valid) {
          let params = {
            version: this.driverForm.version,
            url: this.driverForm.fileList[0].response.url
          };
          addVersion(params).then(() => {
            this.$message.success("司机版本上传成功");
            this.fetchDriverVersion();
          });
        } else {
          this.$message.error("请完善司机表单信息");
          return false;
        }
      });
    },
    submitDiaoduForm() {
      this.$refs.diaoduForm.validate((valid) => {
        if (valid) {
          let params = {
            version: this.diaoduForm.version,
            url: this.diaoduForm.fileList[0].response.url
          };
          addDiaoduVersion(params).then(() => {
            this.$message.success("调度版本上传成功");
            this.fetchDiaoduVersion();
          });
        } else {
          this.$message.error("请完善调度表单信息");
          return false;
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.version-manager-container {
  padding: 20px 0;
  
  .mb-20 {
    margin-bottom: 20px;
  }
  
  .version-card {
    border-radius: 8px;
    transition: all 0.3s;
    height: 100%;
    
    &:hover {
      transform: translateY(-5px);
      box-shadow: 0 15px 30px rgba(0, 0, 0, 0.1);
    }
    
    .card-header {
      display: flex;
      justify-content: space-between;
      align-items: center;
      margin-bottom: 10px;
      
      .header-left {
        display: flex;
        align-items: center;
        
        .header-icon {
          font-size: 32px;
          color: #409EFF;
          margin-right: 15px;
          background: rgba(64, 158, 255, 0.1);
          padding: 10px;
          border-radius: 8px;
        }
        
        .header-title {
          h3 {
            margin: 0 0 8px 0;
            font-size: 18px;
            font-weight: 600;
            color: #303133;
          }
          
          .version-tag {
            color: #606266;
            font-size: 14px;
          }
        }
      }
    }
    
    .version-form {
      margin-top: 20px;
      
      .upload-container {
        width: 100%;
        
        .upload-tip {
          font-size: 12px;
          color: #909399;
          margin-top: 10px;
        }
      }
      
      .submit-btn {
        width: 100%;
        margin-top: 10px;
        border-radius: 4px;
        padding: 12px 20px;
        font-weight: 500;
      }
    }
  }
}
</style>
